<?php
// version 0.7 
// Lastest update 29 Mar 09
// Author Chanaphan Prasomwong
session_start();
/**
 * callback create cv module
 * return (string) of HTML form
 */

function qmr(){
	global $user;
	$form = drupal_get_form('qualificationmatrixreport_nameform');

	return $form;
}


//Generate Form
function qualificationmatrixreport_nameform() {

$result=db_query('SELECT distinct job_position_id FROM apply_detial');
while ($row = db_fetch_array($result)) {
	 //$rows[substr($row[job_position_id],3)] = $row[job_position_id];
	 $rows[ $row[job_position_id]] = $row[job_position_id];

	//$rows[] = $row;
}

//print_r($rows);

 $count=2000;
	$i=1;
	$today = getdate(time()); 
	for($count;$count<=$today['year'];$count++) {	  
      $dates[$i]= $count;
	  $i++;
	}
//setting
	$form['criteria'] = array(
		'#title' => t('Select'),
		'#type' => 'fieldset',		
		'#collapsible' => TRUE,
		'#weight' => 1,
		'#collapsed' => FALSE
	);
	$form['criteria']['job_id'] = array(
		'#title' => t('Job Type'),
		'#type' => 'select',
		'#description' => t(''),
		'#options' => $rows,//array( 'SOM-23' => t('SOM-23'), 'SERD-101' => t('SERD-101'), 'SET-201' => t('SET-201')),
	);
/*	$form['year_options'] = array(
		'#type' => 'value',
		'#value' => $dates
		);
	
	$form['criteria']['selected_year'] = array(
		'#title' => t('Year'),
		'#type' => 'select',
		'#description' => t(''),
		'#options' => $form['year_options']['#value']
	);
*/
	$form['submit'] = array(
	'#type' => 'submit',
	'#weight' => 2,
	'#value' => t('Submit')
	);
	$form['#submit'][] = 'gen_qm';
return $form;
}

//Get command from Submit
function gen_qm($node,$form) {
	$_SESSION['jobID']=$node['criteria']['job_id']['#value'];
	//echo $_SESSION['jobID'];
	return  drupal_goto($path = 'qmr/gen_qmr', $query = 'jobid='.$_SESSION['jobID'], $fragment = NULL, $http_response_code = 302);
	
}

function birthday ($birthday)
  {
    list($year,$month,$day) = explode("-",$birthday);
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
    elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
  }


//call back from menu
function gen_qmr(){
	//echo $_GET['jobid'];
	global $user;
	$form = drupal_get_form('qualificationmatrixreport_nameform');
	
	
	$result=db_query('SELECT count(a.job_position_id) FROM apply_detial a where a.job_position_id="'.$_GET['jobid'].'"');
	//echo db_result($result);
	//$output = theme('', '','');
	$count = db_result($result);
	//echo $count;
	if($count>0) 
	{
		$header = array(
		array(  'data' => t('Name/Nationality/Age'),  'field' => 'person'),
		array(  'data' => t('Education'),     'field' => 'education'),
		array(  'data' => t('Most recently work experience'),     'field' => 'work'),
		array(  'data' => t('Relevant Training'),     'field' => 'training'),
	);
		//$result=db_query('SELECT distinct c.first_name,c.middle_name,c.last_name ,c.nationality, c.dob, w.year,w.company,w.position,o.training FROM apply_detial a, cv_application c left join work_experience w on c.id = w.candidate_id left join other_qualification o on c.id = o.candidate_id where a.job_position_id="'.$_GET['jobid'].'" AND o.id=1;');
		$result=db_query('SELECT distinct first_name,middle_name,last_name ,nationality, dob, year,degree,university,year_work,position,company,description,training FROM apply_detial a, cv_application c where a.candidate_id = c.id AND a.job_position_id="'.$_GET['jobid'].'"');
		$string ='';
		$string = '<table><tr><td>Name/Nationality/Age</td><td>Education</td><td>Most recently work experience</td><td>Relevant Training</td></tr><tr><td>.</td><td></td><td></td><td></td></tr>';
		while ($row = db_fetch_array($result)) {
			//$year = (int)substr($row['dob'],0,4);
			//echo $year;
		  $rows[] = array($row['first_name'].' '.$row['middle_name'].' '.$row['last_name'].'/ '.$row['nationality'].' / '.birthday($row['dob']),$row['degree'].' '.$row['university'], $row['year'].' '.$row['company'].' '.$row['position'],$row['training']);
		  $string .= '<tr><td>'.$row['first_name'].' '.$row['middle_name'].' '.$row['last_name'].'/ '.$row['nationality'].' / '.birthday($row['dob']).'</td><td>'.$row['degree'].' '.$row['university'].'</td><td>'. $row['year'].' '.$row['company'].' '.$row['position'].'</td><td>'.$row['training'].'</td></tr>';
		}
	$string .='</table>';
	//temp string test table
	//$string ='<table><tr> <td>row i, col 1</td>  <td>row 1, col 2</td><td> row 1, col 3</td></tr><tr><td>row 2 , col1 </td> <td>row 2, col2</td> <td>row 2,col 3</td> </tr></table>';

	$_SESSION['table_detail'] = $string;
	table_gen();
	}

	$output = theme('table', $header, $rows);
	if($count>0)
	{
		$result=db_query('SELECT title FROM node where nid="'.substr($_GET['jobid'],3).'"');
		$job_des = db_result($result);
		$job_header = $job_des." (".$_GET['jobid'].")";

		$result=db_query('SELECT nid FROM node where type="preview" AND title="'.$job_header.'"');
		$nid= db_result($result);

		//echo $job_des;
		if($nid !=0)
		{
			$result = db_query('DELETE FROM node Where nid='.$nid);
			//$result2 = db_result($result);

		}
			$output .= '<A href="?q=node/add/preview&jobid='.$_GET['jobid'].'"> Preview </A>';

			//$output .= '<A href="?q=node/'.$nid.'"> Preview </A>';

	}


	return $form.t('<br>'.$output);//.drupal_get_form('table_form');
}

function table_gen(){ 
	global $user;
	//print $_SESSION['table_detail'];
	$result=db_query('SELECT count(job_position_id) FROM temp_print_qm where job_position_id="'.$_GET['jobid'].'"');
	//echo db_result($result);
	if(db_result($result)!=0)
	{
		 $result = db_query('Delete From temp_print_qm Where job_position_id="'.$_GET['jobid'].'"');
	}
	else

	db_query('INSERT INTO temp_print_qm(job_position_id,table_detail)'."VALUES ('%s','%s')",
		$_GET['jobid'],
		$_SESSION['table_detail']
		);

	//$form['header'] = array('#type' => 'textfield','#title' => t('header'),'#size' => 6,'#required' => TRUE,'#default_value' => $_GET['jobid'],);
	//$form['body'] = array('#type' => 'textfield','#title' => t('body'),'#size' => 6,'#required' => TRUE,'#default_value' =>$string,);
}

function goto_preview1($node,$form)
{
	return drupal_goto($path = 'node/add/page', $query = 'jobid='.$_SESSION['jobID'],'QMtable='.$string, $fragment = NULL, $http_response_code = 302);
}

